package com.zjz.studynotes;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.activity.ComponentActivity;
import androidx.annotation.Nullable;

import com.zjz.studynotes.bean.LoginUserInfo;
import com.zjz.studynotes.db.StudyNotesDb;

import java.util.List;

/**
 * Description: 登录页面
 * Author: zjz
 * Date:2025/5/5 13:06
 */
public class LoginActivity extends ComponentActivity {
    private EditText mEtUserName;
    private EditText mEtPassword;
    private Button mBtnLogin;
    private TextView mTvRegister;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_login);

        mEtUserName = findViewById(R.id.et_user_name);
        mEtPassword = findViewById(R.id.et_password);
        mBtnLogin = findViewById(R.id.btn_login);
        mTvRegister = findViewById(R.id.tv_register);


        mBtnLogin.setOnClickListener(view -> {
            login();
        });

        mTvRegister.setOnClickListener(view -> {
            Intent intent = new Intent(this, RegisterActivity.class);
            startActivity(intent);
        });
    }


    private void login() {
        if (TextUtils.isEmpty(mEtUserName.getText().toString())) {
            Toast.makeText(this, "请输入用户名", Toast.LENGTH_SHORT).show();
            return;
        }

        if (TextUtils.isEmpty(mEtPassword.getText().toString())) {
            Toast.makeText(this, "请输入密码", Toast.LENGTH_SHORT).show();
            return;
        }

        List<LoginUserInfo> list = StudyNotesDb.getInstance(this).getLoginUerInfoDao().queryInfo(mEtUserName.getText().toString(), mEtPassword.getText().toString());
        if (list == null || list.size() == 0) {
            Toast.makeText(this, "用户名或者密码错误", Toast.LENGTH_SHORT).show();
            return;
        }

        Toast.makeText(this, "登录成功", Toast.LENGTH_SHORT).show();
        startActivity(new Intent(this, MainActivity.class));
    }
}
